﻿@foreach (var item in Informations)
{
    if (item.Name == "生日")
    {
        <MCol Cols="12" Md="6">
            <CnGalWebSite.Components.Inputs.MasaDatePicker NullableValue="Birthday" NullableValueChanged="BirthdayChanged" ShowDate ShowTime="false" Nullable="true" Label="生日" />
         </MCol>
    }
    else if (item.Name == "性别")
    {
        <MCol Cols="12" Md="6">
            <CnGalWebSite.Components.Inputs.MasaSelecte Value="Gender" ValueChanged="GenderChanged" TValue="GenderType" Label="性别" />
        </MCol>
    }
    else
    {
        <MCol Cols="12" Md="6">
            <MTextField @bind-Value="item.Value" Label="@item.Name" />
        </MCol>
    }

}

@code {
    [Parameter]
    public List<EditInformationModel> Informations { get; set; }

    GenderType Gender;
    DateTime? Birthday;

    protected override void OnInitialized()
    {
        if (Informations.Any(s => s.Name == "性别"))
        {
            Gender = Informations.First(s => s.Name == "性别").Value.ToEnumValue<GenderType>();
        }
        if (Informations.Any(s => s.Name == "生日"))
        {
            var text = Informations.First(s => s.Name == "生日").Value;
            if (string.IsNullOrWhiteSpace(text))
            {
                Birthday = null;
            }
            else
            {
                Birthday = DateTime.ParseExact(text, "M月d日", null);
            }
        }

    }

    public void GenderChanged(GenderType value)
    {
        Gender = value;
        Informations.First(s => s.Name == "性别").Value = value.GetDisplayName();
    }

    public void BirthdayChanged(DateTime? value)
    {
        Birthday = value;
        Informations.First(s => s.Name == "生日").Value = value?.ToString("M月d日");
    }
}
